﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using log4net;
using log4net.Appender;
using System.Net.Mail;
using HelloMVC.Model;
using HelloMVC.Interface.Repository;

namespace HelloMVC.Areas.DashBoard.Controllers
{
    public class AgileController : Controller
    {
        //
        // GET: /DashBoard/Agile/
        public static IList<BoardData> BoardItemList = new List<BoardData> { 
            new BoardData{BoardId = 1, BoardName = "Planning Board", BoardAction = "Index"},
            new BoardData{BoardId = 2, BoardName = "Task Board", BoardAction = "TaskBoard"},
            new BoardData{BoardId = 3, BoardName = "Chart Board", BoardAction = "ChartBoard"},
            new BoardData{BoardId = 4, BoardName = "Released Board", BoardAction = "ReleasedBoard"}
        };

        #region vars
        private readonly ILog _logger = LogManager.GetLogger("HelloMVC");
        private readonly IIssueRepository _issueRepository;
        private readonly IProjectRepository _projectRepository;
        #endregion

        public AgileController(IIssueRepository issueRepository, IProjectRepository projectRepository)
        {
            _issueRepository = issueRepository;
            _projectRepository = projectRepository;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Index(Int32? id)
        {
            var projectId = id.HasValue ? id.Value : 1;
            var board = new BoardData { 
                BoardId = BoardItemList[0].BoardId,
                BoardAction = BoardItemList[0].BoardAction,
                BoardName = BoardItemList[0].BoardName,
                ProjectId = projectId, 
                ProjectName = _projectRepository.QueryProject(projectId).Name };
            return View(board);
        }

        /// <summary>
        /// Board Id = 2
        /// </summary>
        /// <returns></returns>
        public ActionResult TaskBoard(Int32? id)
        {
            var projectId = id.HasValue ? id.Value : 1;
            var taskBoard = new TaskboardData
                                {
                                    BoardId = BoardItemList[1].BoardId,
                                    BoardAction = BoardItemList[1].BoardAction,
                                    BoardName = BoardItemList[1].BoardName,
                                    ProjectId = projectId,
                                    ProjectName = _projectRepository.QueryProject(projectId).Name,
                                    StepFamilies = new List<StepFamily>(), 
                                    OthersIssue = _issueRepository.CombineOtherIssues(projectId)
                                };
            var parentIssues = _issueRepository.QueryProjectParentIssues(projectId);

            foreach (var parentIssue in parentIssues)
            {
                taskBoard.StepFamilies.Add(_issueRepository.CombineStepFamily(parentIssue));
            }

            return View(taskBoard);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public PartialViewResult TaskboardPreview()
        {
            return PartialView("BoardPreview");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public PartialViewResult BoardList(Int32 id, Int32 boardId)
        {
            var result = BoardItemList.Where(m => m.BoardId != boardId).ToList();
            result.ForEach(m => m.ProjectId = id);
            return PartialView("BoardList", result);
        }
    }
}
